//
//  SCCommitView.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/24.
//  Copyright © 2016年 Gcx. All rights reserved.
//
#define colorbdbdbd [UIColor colorWithRed:189.0/255 green:189.0/255 blue:189.0/255 alpha:1.0f]
#define Colord3d3d3 [UIColor colorWithRed:211.0/255 green:211.0/255 blue:211.0/255 alpha:1.0f]
#define Color0885d8 [UIColor colorWithRed:8.0/255 green:133.0/255 blue:216.0/255 alpha:1.0f]
#define Color1b97e9 [UIColor colorWithRed:27.0/255 green:151.0/255 blue:233.0/255 alpha:1.0f]


#import "SCCommitView.h"
#import "SCTypeBtn.h"
#import "SCBlockBtn.h"

@interface SCCommitView(){

    UILabel *_placeHolder;
    SCBlockBtn *_succeedBtn;
    UITextView *_textView;
    SCTypeBtn *_myBtn;
}

@end

@implementation SCCommitView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.autoresizesSubviews = NO;
        [self creatUI];
    }
    return self;
}

-  (void)creatUI{

    self.backgroundColor = BGC;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SIZEWIDTH, ScaleSize(909*SCALE6P))];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    UIView *xian = [[UIView alloc]initWithFrame:CGRectMake(0, ScaleSize(909*SCALE6P)-0.5, SIZEWIDTH, 0.5)];
    xian.backgroundColor = colordbdbdb;
    [view addSubview:xian];
    
    _textView=[[UITextView alloc]initWithFrame:CGRectMake(ScaleSize(33), ScaleSize(20), SIZEWIDTH- ScaleSize(33)*2,ScaleSize(852*SCALE6P))];
    _textView.font = NormalFont(42);
    _textView.textColor = Color333333;
    _textView.delegate = self;
    _placeHolder=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, SIZEWIDTH- ScaleSize(33)*2, 25)];
    _placeHolder.text=@"我们很重视您的宝贵建议和反馈";
    _placeHolder.font= NormalFont(42);
    _placeHolder.textColor=[UIColor lightGrayColor];
    [_textView addSubview:_placeHolder];
    [view addSubview:_textView];
    
    _myBtn = [[SCTypeBtn alloc]initWithFrame:CGRectMake(ScaleSize(60), CGRectGetMaxY(_textView.frame)+ScaleSize(96), SIZEWIDTH - ScaleSize(60)*2, ScaleSize(140))];
    [_myBtn setTitle:@"提交" forState:UIControlStateNormal];
    [_myBtn addTarget:self action:@selector(btnClikced) forControlEvents:UIControlEventTouchUpInside];
    
    _myBtn.isCanUse = NO;
    
    [self addSubview:_myBtn];
    
    
}

- (void)btnClikced{
    
    [self loadRequestData];
}

#pragma mark -- 数据请求
- (void)loadRequestData{
    
    NSDictionary *dic = @{@"oauth_token":@"815bffb68e2ea44e38cf16a98a5a29a4",@"oauth_token_secret":@"8e84bc64f03d73b5c75406ef1d7349f7",@"ztnr":_textView.text};
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];

    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_addFeedback
                                                        params:params];
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        if ([resp.originalDictionary[@"status"]integerValue] == 0) {
            
            _textView.text = @"";
            
            _placeHolder.hidden = NO;

            [_textView resignFirstResponder];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"optionListRefresh" object:nil];
            
            [[JLActionManager sharedInstance]showHUDWithLabelText:resp.originalDictionary[@"statusCode"] imageView:SucceedImage inView:self];
            
        } else {
        
            [[JLActionManager sharedInstance]showHUDWithLabelText:resp.originalDictionary[@"statusCode"] imageView:FailImage inView:self];

        }
        
    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
    }];
}

-(void)textViewDidBeginEditing:(UITextView *)textView{
    
    _placeHolder.hidden = YES;
    
}


- (void)textViewDidChange:(UITextView *)textView{
    
    NSString *str = [textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (str.length == 0) {
        
        _placeHolder.hidden = NO;
        
        _myBtn.isCanUse = NO;
        
    }else{
        
        _placeHolder.hidden = YES;

        _myBtn.isCanUse = YES;
    }
    
}


@end
